package com.example.tetris.slice;

import com.example.tetris.MainAbility;
import com.example.tetris.Question;
import com.example.tetris.Recording;
import com.example.tetris.ResourceTable;
import com.example.tetris.beans.MyCommondialog;
import com.example.tetris.beans.MyToastdialog;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

public class MainSlice extends AbilitySlice {
    Text text;
    Button button_start;
    Button button_recording;
    Button button_question;
    Button button_close;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main2);
//        text=(Text)findComponentById(ResourceTable.Id_text_hello);
//        // 跑马灯效果
//        text.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
//        // 始终处于自动滚动状态
//        text.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
//        // 启动跑马灯效果
//        text.startAutoScrolling();

        button_start = (Button)findComponentById(ResourceTable.Id_main_start);

        if(button_start!=null){
            button_start.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    //获得弹框布局对象
                    DirectionalLayout dl=(DirectionalLayout)LayoutScatter.getInstance(getContext())
                            .parse(ResourceTable.Layout_cmddialog_start,null,false);
                    //装载弹框
                    MyCommondialog commondialog=new MyCommondialog(getContext(),dl);
                    //获得经典模式按钮对象
                    Button bt_classic=(Button) dl.findComponentById(ResourceTable.Id_classic);
                    //获得困难模式按钮对象
                    Button bt_dif=(Button) dl.findComponentById(ResourceTable.Id_difficulty);
                    //获得残局模式按钮对象
                    Button bt_endgm=(Button) dl.findComponentById(ResourceTable.Id_endgame);

                    //第一个点击事件
                    if(bt_classic!=null){
                        bt_classic.setClickedListener(new Component.ClickedListener() {
                            @Override
                            public void onClick(Component component) {
                             Intent intent = new Intent();
                                intent.setParam("mode_flag","classic");
                    Operation operation = new Intent.OperationBuilder().withDeviceId("")
                            //空代表默认路由，也就是默认的slice
                            .withAction("")
                            .withBundleName(getBundleName())
                            .withAbilityName(MainAbility.class.getName())
                            .build();
                    intent.setOperation(operation);
                    //启动
                    startAbility(intent);
                    //销毁弹框
                    commondialog.destroy();
                            }
                        });
                    }
                    //第二个点击事件
                    if(bt_dif!=null){
                        bt_dif.setClickedListener(new Component.ClickedListener() {
                            @Override
                            public void onClick(Component component) {

                                Intent intent = new Intent();
                                intent.setParam("mode_flag","difficulty");
                                Operation operation = new Intent.OperationBuilder().withDeviceId("")
                                        //空代表默认路由，也就是默认的slice
                                        .withAction("")
                                        .withBundleName(getBundleName())
                                        .withAbilityName(MainAbility.class.getName())
                                        .build();
                                intent.setOperation(operation);
                                //启动
                                startAbility(intent);
                                //销毁弹框
                                commondialog.destroy();

                            }
                        });
                    }
                    //第三个点击事件
                    if(bt_endgm!=null){
                        bt_endgm.setClickedListener(new Component.ClickedListener() {
                            @Override
                            public void onClick(Component component) {

                                Intent intent = new Intent();
                                intent.setParam("mode_flag","endgm");
                                Operation operation = new Intent.OperationBuilder().withDeviceId("")
                                        //空代表默认路由，也就是默认的slice
                                        .withAction("")
                                        .withBundleName(getBundleName())
                                        .withAbilityName(MainAbility.class.getName())
                                        .build();
                                intent.setOperation(operation);
                                //启动
                                startAbility(intent);
                                //销毁弹框
                                commondialog.destroy();

                            }
                        });
                    }





                }
            });
        }

        button_recording = (Button)findComponentById(ResourceTable.Id_main_recording);

        if(button_recording!=null){
            button_recording.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    //获得弹框布局对象
                    DirectionalLayout dl=(DirectionalLayout)LayoutScatter.getInstance(getContext())
                            .parse(ResourceTable.Layout_cmddialog_to_recording,null,false);
                    //装载弹框
                    MyCommondialog commondialog=new MyCommondialog(getContext(),dl);
                    //获得经典模式记录按钮对象
                    Button bt_classic=(Button) dl.findComponentById(ResourceTable.Id_rd_classic);
                    //获得困难模式记录按钮对象
                    Button bt_dif=(Button) dl.findComponentById(ResourceTable.Id_rd_difficulty);

                    if(bt_classic!=null){
                        bt_classic.setClickedListener(new Component.ClickedListener() {
                            @Override
                            public void onClick(Component component) {
                                Intent intent = new Intent();
                                intent.setParam("mode_flag","classic");
                                Operation operation = new Intent.OperationBuilder().withDeviceId("")
                                        //空代表默认路由，也就是默认的slice
                                        .withAction("")
                                        .withBundleName(getBundleName())
                                        .withAbilityName(Recording.class.getName())
                                        .build();
                                intent.setOperation(operation);
                                //启动
                                startAbility(intent);
                                commondialog.destroy();
                            }
                        });
                    }

                    if(bt_dif!=null){
                        bt_dif.setClickedListener(new Component.ClickedListener() {
                            @Override
                            public void onClick(Component component) {
                                Intent intent = new Intent();
                                intent.setParam("mode_flag","difficulty");
                                Operation operation = new Intent.OperationBuilder().withDeviceId("")
                                        //空代表默认路由，也就是默认的slice
                                        .withAction("")
                                        .withBundleName(getBundleName())
                                        .withAbilityName(Recording.class.getName())
                                        .build();
                                intent.setOperation(operation);
                                //启动
                                startAbility(intent);
                                commondialog.destroy();
                            }
                        });
                    }

                }
            });
        }

        button_question = (Button)findComponentById(ResourceTable.Id_main_question);
        if(button_question!=null){
            button_question.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    Intent intent = new Intent();
                    Operation operation = new Intent.OperationBuilder().withDeviceId("")
                            //空代表默认路由，也就是默认的slice
                            .withAction("")
                            .withBundleName(getBundleName())
                            .withAbilityName(Question.class.getName())
                            .build();
                    intent.setOperation(operation);
                    //启动
                    startAbility(intent);
                }
            });
        }

        button_close = (Button)findComponentById(ResourceTable.Id_main_close);
        if(button_close!=null){
            button_close.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    MyToastdialog.showTip(getContext(),"退出游戏");
                    terminateAbility();
                }
            });
        }




    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
